<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\Carousels;
use App\Http\Resources\Categories;
use App\Http\Resources\Notices;
use App\Http\Resources\Products;
use App\Models\Carousel;
use App\Models\Category;
use App\Models\Coupon;
use App\Models\Notice;
use App\Models\Product;
use App\Services\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    /**
     * 首页
     */
    public function index(Request $request)
    {
        $result = [];

        $categories = Category::query()->doesntHave('children')->get();

        $result['category'] = Categories::collection($categories);

        $carousels = Carousel::query()
            ->latest()
            ->where('status', 1)
            ->get();

        $result['carousel'] = Carousels::collection($carousels);

        $notices = Notice::query()
            ->latest()->where('status', 1)
            ->whereIn('type', [Notice::TYPE_PRODUCT, Notice::TYPE_CATEGORY])
            ->get();

        $result['notice'] = Notices::collection($notices);

        $products = Product::query()
            ->orderBy('count_sales', 'desc')
            ->limit(10)
            ->get();

        $result['products'] = Products::collection($products);

        $coupons = Notice::query()
            ->where('status', 1)
            ->latest()->where('type', Notice::TYPE_COUPON)
            ->get();

        $result['coupons'] = Notices::collection($coupons);

        return Api::msg('api.home.index', $result);
    }
}
